package com.neusoft.elm.dao.impl;

import com.neusoft.elm.dao.UserDao;
import com.neusoft.elm.po.User;
import com.neusoft.elm.util.DBUtil;

import javax.xml.transform.Result;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements UserDao {
    private Connection conn=null;
    private PreparedStatement prep=null;
    private ResultSet res=null;


//    注册用户
    @Override
    public int saveUser(User user) throws Exception{
        try {
            conn= DBUtil.getConnection();
            String sql="insert into user values(?,?,?,?,?,?)";
            prep=conn.prepareStatement(sql);
//            设置参数
            prep.setString(1,user.getUserId());
            prep.setString(2,user.getPassword());
            prep.setString(3,user.getUsername());
            prep.setInt(4,user.getUserSex());
            prep.setString(5,user.getUserImg());
            prep.setInt(6,user.getDelTag());
            int n=prep.executeUpdate();
            return  n;


        } catch (SQLException e) {
            e.printStackTrace();
            throw e;
        } finally {
            prep.close();
        }


    }
//    登陆
    @Override
    public User getUserByIdByPass(String userId,String password) throws Exception{
        try {
            conn=DBUtil.getConnection();
            String sql="select * from user where userId=? and password=? ";
            prep=conn.prepareStatement(sql);
            prep.setString(1,userId);
            prep.setString(2,password);
            res=prep.executeQuery();
            User user=null;
            if (res.next()){
                user=new User();
                user.setDelTag(res.getInt("delTag"));
                user.setUserImg(res.getString("userImg"));
                user.setUserSex(res.getInt("userSex"));
                user.setUsername(res.getString("userName"));
                user.setPassword(res.getString("password"));
                user.setUserId(res.getString("password"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw e;
        } finally {
            prep.close();
        }


return new User();

    }
//    根据id查询用户是否注册重复
    @Override
    public User getUserById(String userId) throws Exception{
        try {
            conn=DBUtil.getConnection();
            String sql="select * from user where id=?";
            prep=conn.prepareStatement(sql);
            prep.setString(1,userId);
            res=prep.executeQuery();

            User user=null;
            if (res.next()){
                user=new User();
                user.setUserId(res.getString("userId"));
                user.setPassword(res.getString("password"));
                user.setUsername(res.getString("username"));
                user.setUserSex(res.getInt("userSex"));
                user.setUserImg(res.getString("userImg"));
                user.setDelTag(res.getInt("delTag"));
            }
            return user;
        } catch (SQLException e) {
            e.printStackTrace();
            throw e;
        } finally {
          DBUtil.close(res,prep);
        }


    }
}
